using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Common;
using CoachingOrganizer.Android.Helpers;
using CoachingOrganizer.Android.Adapters;
using CoachingOrganizer.Logic.Models.Tools.MoodLevel;
using CoachingOrganizer.Android.Activities.Tools.MoodLevel;
using CoachingOrganizer.Logic.Models.Tools.EmotionsPack;
using CoachingOrganizer.Android.Activities.Tools.EmotionsPack;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using CoachingOrganizer.Android.Activities.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Activities
{
    [Activity(Label = "@string/ApplicationName")]
    public class GoalDetailsActivity : Activity
    {
        private GoalModel _model;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            _model = UIHelper.GetActivityModel<GoalModel>(this, bundle);
            SetContentView(Resource.Layout.GoalDetailsLayout);
            InitLayout();
            Init();
            HookModel();
        }

        protected override void OnResume()
        {
            base.OnResume();
            //HookModel();
        }

        protected override void OnPause()
        {
            base.OnPause();
            //UnhookModel();
        }

        private void InitLayout()
        {
            TabHost tabHost = FindViewById<TabHost>(Resource.Id.goalTabHost);
            tabHost.Setup();
            TabHost.TabSpec spec = tabHost.NewTabSpec("tab1");
            spec.SetContent(Resource.Id.goalCommonLinearLayout);
            spec.SetIndicator(Resources.GetString(Resource.String.GoalCommonTabTitle));
            tabHost.AddTab(spec);
            spec = tabHost.NewTabSpec("tab2");
            spec.SetContent(Resource.Id.goalSupplyLinearLayout);
            spec.SetIndicator(Resources.GetString(Resource.String.GoalSupplyTabTitle));
            tabHost.AddTab(spec);
        }

        private async void Init()
        {
            using (System.Threading.CancellationTokenSource cancellation = new System.Threading.CancellationTokenSource())
                await _model.LoadToolsAsync(cancellation.Token);
        }

        private void HookModel()
        {
            _model.CurrentToolChanged += Model_CurrentToolChanged;
            EditText descriptionEditText = FindViewById<EditText>(Resource.Id.descriptionEditText);
            descriptionEditText.Text = _model.Description;
            descriptionEditText.TextChanged += (sender, e) => _model.Description = descriptionEditText.Text;
            EditText expectedResultVolumeEditText = FindViewById<EditText>(Resource.Id.expectedResultVolumeEditText);
            expectedResultVolumeEditText.Text = _model.ExpectedResultVolume;
            expectedResultVolumeEditText.TextChanged += (sender, e) => _model.ExpectedResultVolume = expectedResultVolumeEditText.Text;
            Button saveGoalDetailsButton = FindViewById<Button>(Resource.Id.saveGoalDetailsButton);
            saveGoalDetailsButton.Click += (sender, e) => _model.Save();
            Spinner toolsSpinner = FindViewById<Spinner>(Resource.Id.toolSpinner);
            Button addToolButton = FindViewById<Button>(Resource.Id.addToolButton);
            addToolButton.Click += (sender, e) =>
            {
                switch (toolsSpinner.SelectedItemPosition)
                {
                    case 0:
                        _model.AddNewMoodLevel();
                        break;
                    case 1:
                        _model.AddNewEmotionsPack();
                        break;
                    case 2:
                        _model.AddNewMoneyCalculation();
                        break;
                    default:
                        break;
                }
            };
            ListView goalToolsListView = FindViewById<ListView>(Resource.Id.goalToolsListView);
            goalToolsListView.Adapter = new ToolsAdapter(this, _model);
        }

        private void UnhookModel()
        {
            throw new NotImplementedException();
        }

        private void Model_CurrentToolChanged(object sender, EventArgs e)
        {
            Type targetType = null;
            if (_model.CurrentTool is MoodLevelModel)
                targetType = typeof(MoodDetailsActivity);
            if (_model.CurrentTool is EmotionsPackModel)
                targetType = typeof(EmotionsPackDetailsActivity);
            if (_model.CurrentTool is MoneyCalculationModel)
                targetType = typeof(MoneyCalculationDetailsActivity);
            if (targetType == null)
                return;
            UIHelper.GoActivity(this, _model.CurrentTool, targetType);
        }
    }
}